<?php
/************************
* Simple News Engine 0.2.3
* author: Ragnaar <ragnaar@km.ru>
* home: http://code.google.com/p/sne-news
* date: 08.06.2011
*********************/
define('_VALID_SNE', '1');
define('SNE_PATH', dirname(__FILE__)); //определяем путь

//шаблоны
$pub_header_tpl = SNE_PATH."/inc/tpl/pub_header.tpl";
$pub_footer_tpl = SNE_PATH."/inc/tpl/pub_footer.tpl";

$title = "Install SimpleNews Engine";
$header = "";
include $pub_header_tpl;

if (file_exists(SNE_PATH.'/inc/config.php')){
    echo "<h4>Configuration file was found!</h4>";
    require_once(SNE_PATH.'/inc/config.php');
} else {
    echo "<h4>Configuration file not found! Configure script manually.</h4>";
}


?>	
<form action="./install.php" method="GET" name="install">
	<label for="dblocation">Database server:</label> <input type="text" name="dblocation" value="<?php echo $dblocation ?>" /><br />
	<label for="dbuser">Database user:</label> <input type="text" name="dbuser" value="<?php echo $dbuser ?>" /><br />
	<label for="dbpassword">Database password:</label> <input type="password" name="dbpassword" value="<?php echo $dbpassword ?>" /><br />
	<label for="dbname">Base name:</label> <input type="text" name="dbname" value="<?php echo $dbname ?>" /><br />
	<label for="dbprefix">Tables prefix:</label> <input type="text" name="dbprefix" value="<?php echo $dbprefix ?>" /><br />
	<input type="submit" value="Make!"/>
</form>
<?php

if($_GET){
    $dblocation = $_GET['dblocation'];
	$dbuser = $_GET['dbuser'];
	$dbpassword = $_GET['dbpassword'];
	
	$dbname = $_GET['dbname'];
	$dbprefix = $_GET['dbprefix'];
	
	$dbcnx = mysql_connect($dblocation,$dbuser,$dbpassword);
	
    if(!$dbcnx) {
        echo "can't connect";
        exit();
    }

    if(!mysql_select_db($dbname,$dbcnx)){
        echo "can't select db";
        exit();
    }

    $query = <<<SQL
    CREATE TABLE `{prefix}news` (
        `id` int(11) NOT NULL auto_increment,
        `title` tinytext NOT NULL,
        `text` text NOT NULL,
        `cut` tinyint(1) NOT NULL default '0',
        `comment` tinyint(1) NOT NULL default '0',
        `image` tinyint(1) NOT NULL default '0',
        `date` datetime NOT NULL default '0000-00-00 00:00:00',
        `pub` tinyint(1) NOT NULL default '0',
        PRIMARY KEY  (`id`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=utf8;
SQL;

    $query = str_replace('{prefix}', $dbprefix, $query);
    mysql_query($query);

    $query = "INSERT INTO `{prefix}news` (`title`, `text`, `cut`, `image`, `date`, `pub`) VALUES ('First good news', 'SimpleNews Engine sucesfuly installed!<hr id=\"cut\">', 0, 0, '".date('Y-m-d H:i:s')."', 1)";
    $query = str_replace('{prefix}', $dbprefix, $query);
    mysql_query($query);

    $query = <<<SQL
    CREATE TABLE `{prefix}images` (
        `id` int(11) NOT NULL auto_increment,
        `name` tinytext NOT NULL,
        `title` tinytext NOT NULL,
        `description` text NOT NULL,
        `pub` tinyint(1) NOT NULL default '0',
        `news_id` int(11) NOT NULL,
        PRIMARY KEY  (`id`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=utf8;
SQL;

    $query = str_replace('{prefix}', $dbprefix, $query);
    mysql_query($query);

    $query = <<<SQL
    CREATE TABLE `{prefix}comments` (
        `id` int(11) NOT NULL auto_increment,
        `num` int(11) NOT NULL,
        `date` datetime NOT NULL default '0000-00-00 00:00:00',
        `socnet` tinytext NOT NULL,
        `nid` int(11) NOT NULL,
        PRIMARY KEY  (`id`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=utf8;
SQL;

    $query = str_replace('{prefix}', $dbprefix, $query);
    mysql_query($query);


}

include $pub_footer_tpl;
?>
